<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;

/**
 * Description of OpSettingController
 *
 * @author PC
 */
class OpSettingController extends AdminBaseController
{

    /**
     * 客服管理
     * @adminMenu(
     *     'name'   => '客服管理',
     *     'parent' => 'admin/Service/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '客服管理',
     *     'param'  => ''
     * )
     */
    public function service()
    {
        // cmf_clear_cache();
        $mall = cmf_get_option('mall');
        $this->assign("mall", $mall);

        $connectPicItem = [];
        $connectPicItem['connect_img']['value'] = $mall['connect_img'];
        $connectPicItem['connect_img']['type'] = "image2";
        $this->assign("array_item", $connectPicItem);

        return $this->fetch('service');
    }

    /**
     * 添加客服提交
     * @adminMenu(
     *     'name'   => '添加客服提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加客服提交',
     *     'param'  => ''
     * )
     */
    public function addServicePost()
    {
        if ($this->request->isPost()) {
            $mall = $this->request->param('mall/a');
            if (empty($mall['connect_img']) || !isset($mall['connect_img']) || strlen($mall['connect_img']) == 0) {
                $this->error('请上传图片');
            }
            cmf_set_option('mall', $mall);
            $this->success(lang('EDIT_SUCCESS'), '');
        }
    }

    /**
     * 默认昵称/头像管理
     * @adminMenu(
     *     'name'   => '默认昵称/头像管理',
     *     'parent' => 'admin/Service/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '默认昵称/头像管理',
     *     'param'  => ''
     * )
     */
    public function avatar()
    {
        $mall = cmf_get_option('mall');
        $this->assign("mall", $mall);
        return $this->fetch('avatar');
    }

    /**
     * 编辑昵称/头像
     * @adminMenu(
     *     'name'   => '编辑昵称/头像',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑昵称/头像',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function avatarEdit()
    {
        $sex = $this->request->param('sex');
        $this->assign("sex", $sex);
        
        $mall = cmf_get_option('mall');
        $this->assign("nick", $sex == 'male' ? 
            (isset($mall['default_male_nick']) ? $mall['default_male_nick'] : '') 
            : (isset($mall['default_female_nick']) ? $mall['default_female_nick'] : ''));

        $avatarItem = [];
        $avatarItem['avatar']['value'] = 
            $sex == 'male' ? 
            (isset($mall['default_male_avatar']) ? $mall['default_male_avatar'] : '') : 
            (isset($mall['default_female_avatar']) ? $mall['default_female_avatar'] : '');
        $avatarItem['avatar']['type'] = "image2";
        $this->assign("avatar_item", $avatarItem);

        return $this->fetch('avatar_edit');
    }

    /**
     * 添加默认昵称/头像管理提交
     * @adminMenu(
     *     'name'   => '添加默认昵称/头像管理提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加默认昵称/头像管理提交',
     *     'param'  => ''
     * )
     */
    public function addAvatarPost()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();

            if (!isset($post['post_nick']) || empty($post['post_nick']) || strlen($post['post_nick']) == 0) {
                $this->error('昵称不能为空');
            }
            if (!isset($post['mall']['avatar']) || empty($post['mall']['avatar']) || strlen($post['mall']['avatar']) == 0) {
                $this->error('头像不能为空');
            }
            
            // $mall = cmf_get_option('mall');
            if ($post['post_sex'] == 'male') {
                $mall['default_male_nick'] = $post['post_nick'];
                $mall['default_male_avatar'] = $post['mall']['avatar'];
                $mall['default_male_date'] = time();                
            }
            else {
                $mall['default_female_nick'] = $post['post_nick'];
                $mall['default_female_avatar'] = $post['mall']['avatar'];
                $mall['default_female_date'] = time();
            }            
            cmf_set_option('mall', $mall);
            $this->success(lang('EDIT_SUCCESS'));
            // $this->fetch('avatar');
        }
    }

    /**
     * 兑换比例管理
     * @adminMenu(
     *     'name'   => '兑换比例管理',
     *     'parent' => 'admin/Service/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '兑换比例管理',
     *     'param'  => ''
     * )
     */
    public function exchangePercent()
    {
        $mall = cmf_get_option('mall');
        $this->assign("mall", $mall);
        return $this->fetch('exchange_percent');
    }

    /**
     * 兑换比例管理提交
     * @adminMenu(
     *     'name'   => '兑换比例管理提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '兑换比例管理提交',
     *     'param'  => ''
     * )
     */
    public function exchangePercentPost()
    {
        if ($this->request->isPost()) {
            $mall = $this->request->param('mall/a');
            $mall['exchange_percent'] = min(100, isset($mall['exchange_percent']) && !empty($mall['exchange_percent']) ? $mall['exchange_percent'] : 0);
            $mall['exchange_percent'] = $mall['exchange_percent'] * 100;
            cmf_set_option('mall', $mall);
            $this->success(lang('EDIT_SUCCESS'), '');
        }
    }

    /**
     * 排行榜人数管理
     * @adminMenu(
     *     'name'   => '排行榜人数管理',
     *     'parent' => 'admin/Service/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '排行榜人数管理',
     *     'param'  => ''
     * )
     */
    public function rankingListCount()
    {
        $rankingList = cmf_get_option('ranking_list');
        $this->assign("ranking_list", $rankingList);
        return $this->fetch('ranking_list_count');
    }

    /**
     * 排行榜人数管理提交
     * @adminMenu(
     *     'name'   => '排行榜人数管理提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '排行榜人数管理提交',
     *     'param'  => ''
     * )
     */
    public function rankingListCountPost()
    {
        if ($this->request->isPost()) {
            $rankingList = $this->request->param('ranking_list/a');
            cmf_set_option('ranking_list', $rankingList);
            $this->success(lang('EDIT_SUCCESS'), '');
        }
    }

}
